package scrapygui;

import com.sun.istack.internal.NotNull;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

class CMDModel {
    List<String> exec(String cmd, @NotNull Consumer<String> error) {
        File dir = new File( new File( "" ).getAbsolutePath() );
        String pathInfo = "所在目录：" + dir.getAbsolutePath() + "\n\n";
                List<String> retList = new ArrayList<>();
        Process p;
        try {
            p = Runtime.getRuntime().exec( "cmd /c " + cmd, null, dir );
        } catch (IOException e) {
            e.printStackTrace();
            error.accept( pathInfo + e.getMessage() );
            return retList;
        }
        try (
                InputStream fis = p.getInputStream();
                InputStreamReader isr = new InputStreamReader( fis, "GBK" );
                BufferedReader br = new BufferedReader( isr )
        ) {
            //exec执行cmd命令
            String line;

            while( ( line = br.readLine() ) != null ) {
                retList.add( line );
                System.out.println( line );
            }
        } catch (IOException e) {
            e.printStackTrace();
            error.accept( pathInfo + e.getMessage() );
        }
        return retList;
    }
}